package org.weso.lodin.datasource.config;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.weso.lodin.exceptions.DocumentBuilderException;
import org.weso.lodin.exceptions.LodinModelException;
import org.weso.lodin.generated.Dataset;
import org.weso.lodin.utils.DatasetXMLBind;
import org.weso.lodin.utils.DocumentBuilderHelper;


public class DatasetTemplateDAOImpl implements DatasetTemplateDAO {

	static ResourceBundle dataset = null; 

	static{
		dataset = ResourceBundle.getBundle(DatasetTemplateDAOImpl.class.getName().toString());
	}

	protected static Logger logger = Logger.getLogger(DatasetTemplateDAOImpl.class);


	public List<Dataset> getDatasets() {
		Enumeration<String> keys = dataset.getKeys();
		List<Dataset> indicatorTypes = new LinkedList<Dataset>();
		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		try {
			while(keys.hasMoreElements()){
				String key = keys.nextElement();
				String xmlFile = dataset.getString(key);	
				logger.info("Loading dataset config from "+key);
				InputStream in = classLoader.getResourceAsStream(xmlFile);
				Document doc;
				doc = DocumentBuilderHelper.getDocumentFromInputStream(in);
				Dataset value = DatasetXMLBind.getInstance().restoreIndicator(doc);
				indicatorTypes.add(value);
			}
		} catch (DocumentBuilderException e) {
			throw new LodinModelException(e, "Loading dataset types");
		}
		return indicatorTypes;
	}



}
